package com.supplychainapi.controller;

/**
 * Created by wangziqi on 2017/9/26.
 */

import com.alibaba.fastjson.JSONObject;
import com.joindata.inf.common.basic.exceptions.ServiceException;
import com.supplychainapi.common.ApiVersion;
import com.supplychainapi.common.RestResponse;
import com.supplychainapi.common.enums.ExceptionMessage;
import com.supplychainapi.domain.LoginCache;
import com.supplychainapi.domain.MaterialListSearch;
import com.supplychainapi.param.TbUnitParam;
import com.supplychainapi.proxy.ValidateHelper;
import com.supplychainapi.service.TbUnitService;
import com.supplychainapi.utils.PathUitls;
import io.swagger.annotations.*;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * 单位
 *
 * @Time 2017年9月25日
 * @author wangziqi
 */
@RequestMapping(PathUitls.ROOT_PHONE + "/{version}/unit")
@RestController
@Api(value = "UnitController", description = "计量单位")
public class UnitController extends BaseController {

    private Logger logger = LogManager.getLogger(this.getClass());

    @Autowired
    TbUnitService unitService;

    /**
     * 新增计量单位
     *
     */
    @ApiVersion(1)
    @ApiOperation(value="新增单位", notes="")
    @RequestMapping(value = "/save", method= RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "fsUnitName", value = "单位名称;swagger", required = true, dataType = "String", defaultValue = ""),
    })
    public RestResponse save(@ApiIgnore LoginCache loginCache,@ApiIgnore @RequestBody TbUnitParam model) throws ServiceException {
        model.setFiStatus((short) 1);
        model.setFsShopGUID(loginCache.getFsShopGUID());
        ValidateHelper.validate(model);
        model.setFsUpdateUserId(loginCache.getFsUserId());
        model.setFsUpdateUserName(loginCache.getFsUserName());
        return unitService.save(model);
    }

    /**
     * 更新
     *
     */
    @ApiVersion(1)
    @ApiOperation(value="更新单位", notes="")
    @RequestMapping(value = "/update", method= RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "fsUnitName", value = "单位名称;swagger", required = true, dataType = "String", defaultValue = ""),
            @ApiImplicitParam(paramType="query", name = "fiStatus", value = "启用禁用;swagger", required = false, dataType = "String", defaultValue = "1"),
    })
    public RestResponse update(@ApiIgnore LoginCache loginCache,@ApiIgnore @RequestBody TbUnitParam model) throws ServiceException{
        model.setFsShopGUID(loginCache.getFsShopGUID());
        ValidateHelper.validate(model);
        model.setFsUpdateUserId(loginCache.getFsUserId());
        model.setFsUpdateUserName(loginCache.getFsUserName());
        return unitService.update(model);
    }

    /**
     * 删除
     *
     */
    @ApiVersion(1)
    @ApiOperation(value="批量删除单位", notes="")
    @RequestMapping(value = "/delete", method= RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "list", value = "代码list;swagger", required = true, dataType = "list", defaultValue = ""),

    })
    public RestResponse delete(@ApiIgnore LoginCache loginCache, @ApiParam("code list") @RequestBody JSONObject json) throws ServiceException {

        if (null == json || !json.containsKey("list")) {
            throw ExceptionMessage.missing_param.getServiceException();
        }
        List list = json.getJSONArray("list");
        return unitService.delete(list,loginCache.getFsShopGUID());
    }

    /**
     * 根据主键查询
     *
     */
    @ApiVersion(1)
    @ApiOperation(value="根据主键查询单位", notes="")
    @RequestMapping(value = "/select", method= RequestMethod.GET)
    public RestResponse select(@ApiIgnore LoginCache loginCache,@ApiParam("主键id") @RequestParam String fsUnitId) throws ServiceException{

        return unitService.selectById(loginCache.getFsShopGUID(),fsUnitId);
    }


    /**
     * 查询所有单位
     */
    @ApiVersion(1)
    @ApiOperation(value = "搜索", notes = "")
    @RequestMapping(value = "/search", method = RequestMethod.POST)
    public RestResponse search(@ApiIgnore LoginCache loginCache, @RequestBody MaterialListSearch search) throws ServiceException {

        return unitService.search(loginCache.getFsShopGUID(), search.getPageNum(), search.getPageSize(), search.getKeyWords());
    }

    /**
     * 查询所有单位
     *
     */
    @ApiVersion(1)
    @ApiOperation(value="查询启用单位", notes="")
    @RequestMapping(value = "/selectActivatAll", method = RequestMethod.GET)
    public RestResponse selectActivatAll(@ApiIgnore LoginCache loginCache) throws ServiceException {

        return unitService.selectActivatAll(loginCache.getFsShopGUID());
    }
}
